package io.rocketman.transport;

import io.rocketman.transport.channel.Channel;
import lombok.Getter;

import java.net.InetSocketAddress;

/**
 * @author He Peng
 * @create 2018-05-25 0:48
 * @update 2018-05-25 0:48
 * @updatedesc : 更新说明
 * @see
 */


@Getter
public class NetworkTransportException extends Exception {

    private Channel channel;
    private InetSocketAddress localAddress;
    private InetSocketAddress remoteAddress;

    public NetworkTransportException(Channel channel , String errorMsg) {
        super(errorMsg);
        this.channel = channel;
        fillSocketAddress(channel);
    }

    public NetworkTransportException(Channel channel , String errorMsg , Throwable cause) {
        super(errorMsg , cause);
        this.channel = channel;
        fillSocketAddress(channel);
    }

    public NetworkTransportException(InetSocketAddress localAddress , InetSocketAddress remoteAddress ,String errorMsg) {
        super(errorMsg);
        setSocketAddress(localAddress , remoteAddress);
    }

    public NetworkTransportException(InetSocketAddress localAddress , InetSocketAddress remoteAddress ,String errorMsg , Throwable cause) {
        super(errorMsg , cause);
        setSocketAddress(localAddress , remoteAddress);
    }

    private void fillSocketAddress(Channel channel) {
        if (channel != null) {
            setSocketAddress(channel.getLocalAddress() , channel.getRemoteAddress());
        }
    }

    private void setSocketAddress(InetSocketAddress localAddress , InetSocketAddress remoteAddress) {
        this.localAddress = localAddress;
        this.remoteAddress = remoteAddress;
    }
}
